package ysq.xyg.recovery.controller;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.model.po.PowerMember;
import ysq.xyg.recovery.model.vo.PowerMemberVo;
import ysq.xyg.recovery.service.PowerMemberService;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 回收金模块-权限用户表controller层
 *
 * @author congcong tao
 * @date 2019-9-18 15:15:22
 */
@RestController
public class PowerMemberController extends BaseController {

    private static Logger log = LoggerFactory.getLogger(PowerMemberController.class);

    @Resource
    private WebMemberService webMemberService;
    @Resource
    private PowerMemberService powerMemberService;
    private PowerMember powerMember;

    /**
     * 分页查询
     *
     * @param powerMemberVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @BaseLog(module = "回收金模块",className = "权限用户表",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/powerMember/page",method = RequestMethod.POST)
    public ReturnEntity page(PowerMemberVo powerMemberVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = powerMemberService.page(powerMemberVo, pageSize, pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @RequestMapping(value = "/powerMember/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id){
        ModelAndView view = new ModelAndView("/recovery/recoveryPowerMemberUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            powerMember = powerMemberService.selectByPrimaryKey(id);
            // 需要用户数据
            if (powerMember != null) {
                WebMember itemWebMember = webMemberService.selectByPrimaryKey(powerMember.getItemMemberId());
                WebMember orderWebMember = webMemberService.selectByPrimaryKey(powerMember.getOrderMemberId());
                WebMember withdrawWebMember = webMemberService.selectByPrimaryKey(powerMember.getWithdrawMemberId());
                WebMember cardWebMember = webMemberService.selectByPrimaryKey(powerMember.getCardMemberId());
                powerMember.setItemMemberCardNo(itemWebMember.getCardNo());
                powerMember.setItemMemberNickName(BaseUtil.decodeBase64(itemWebMember.getNickName()));
                powerMember.setOrderMemberCardNo(orderWebMember.getCardNo());
                powerMember.setOrderMemberNickName(BaseUtil.decodeBase64(orderWebMember.getNickName()));
                powerMember.setWithdrawMemberCardNo(withdrawWebMember.getCardNo());
                powerMember.setWithdrawMemberNickName(BaseUtil.decodeBase64(withdrawWebMember.getNickName()));
                powerMember.setCardMemberCardNo(cardWebMember.getCardNo());
                powerMember.setCardMemberNickName(BaseUtil.decodeBase64(cardWebMember.getNickName()));
            }
            view.addObject("powerMember", powerMember);
        } else {
            // 从页面跳转过来的，直接查看
            List<PowerMember> powerMembers = powerMemberService.acceptList(new PowerMemberVo());
            if (powerMembers != null && powerMembers.size() > 0) {
                powerMember = powerMembers.get(0);
                if (powerMember != null) {
                    WebMember itemWebMember = webMemberService.selectByPrimaryKey(powerMember.getItemMemberId());
                    WebMember orderWebMember = webMemberService.selectByPrimaryKey(powerMember.getOrderMemberId());
                    WebMember withdrawWebMember = webMemberService.selectByPrimaryKey(powerMember.getWithdrawMemberId());
                    WebMember cardWebMember = webMemberService.selectByPrimaryKey(powerMember.getCardMemberId());
                    powerMember.setItemMemberCardNo(itemWebMember.getCardNo());
                    powerMember.setItemMemberNickName(BaseUtil.decodeBase64(itemWebMember.getNickName()));
                    powerMember.setOrderMemberCardNo(orderWebMember.getCardNo());
                    powerMember.setOrderMemberNickName(BaseUtil.decodeBase64(orderWebMember.getNickName()));
                    powerMember.setWithdrawMemberCardNo(withdrawWebMember.getCardNo());
                    powerMember.setWithdrawMemberNickName(BaseUtil.decodeBase64(withdrawWebMember.getNickName()));
                    powerMember.setCardMemberCardNo(cardWebMember.getCardNo());
                    powerMember.setCardMemberNickName(BaseUtil.decodeBase64(cardWebMember.getNickName()));
                }
                view.addObject("powerMember", powerMember);
            }
        }
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @BaseLog(module = "回收金模块",className = "权限用户表",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/powerMember/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return powerMemberService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param powerMemberVo Vo对象
     * @author congcong tao
     * @date 2019-9-18 15:15:22
     */
    @BaseLog(module = "回收金模块",className = "权限用户表",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/powerMember/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,PowerMemberVo powerMemberVo){
        return powerMemberService.addOrUpdate(powerMemberVo,acceptCurrentUser(request));
    }
}